import 'package:flutter/material.dart';
import 'package:todo/constant.dart';
import 'package:todo/screens/component/task_card.dart';
import 'package:get/get.dart';
import 'package:todo/controller/checktask_controller.dart';

class HomeScreen extends StatefulWidget {
  HomeScreen({Key key}) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final _pageScroll = PageController(viewportFraction: 0.85, keepPage: true);
  List<Color> currentColor = backColor[0];

  final checkTaskController = Get.put(CheckTaskController());
  String greeting() {
    var hour = DateTime.now().hour;
    print(hour);
    if (hour < 12) {
      return "上午";
    } else if (hour < 17) {
      return "下午";
    } else {
      return "晚上";
    }
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return AnimatedContainer(
      decoration: BoxDecoration(
        gradient: LinearGradient(
            begin: Alignment.topRight,
            end: Alignment.bottomLeft,
            colors: currentColor),
      ),
      duration: Duration(milliseconds: 200),
      curve: Curves.easeIn,
      child: Container(
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            elevation: 0,
            centerTitle: true,
            title: Text("缤纷待办"),
          ),
          body: SingleChildScrollView(
            child: Container(
              width: double.infinity,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: const EdgeInsets.symmetric(
                      horizontal: kDefaultPaddin * 2,
                      vertical: kDefaultPaddin,
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "${greeting()}好",
                          style: Theme.of(context)
                              .textTheme
                              .headline4
                              .copyWith(color: Colors.white),
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(
                              vertical: kDefaultPaddin),
                          child: Text(
                            "今天又是美好的一天\n您当前有 ${checkTaskController.countTodoToday} 个待办任务",
                            style: Theme.of(context)
                                .textTheme
                                .bodyText1
                                .copyWith(
                                    color: Colors.white,
                                    fontWeight: FontWeight.w300),
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 400,
                    child: Padding(
                        padding: EdgeInsets.zero,
                        child: Obx(() {
                          return PageView.builder(
                            allowImplicitScrolling: true,
                            controller: _pageScroll,
                            itemCount: checkTaskController.taskModelList.length,
                            onPageChanged: (index) {
                              setState(() {
                                currentColor = backColor[index];
                              });
                            },
                            itemBuilder: (context, index) {
                              return TaskCard(
                                size: size,
                                task: checkTaskController.taskModelList[index],
                                press: () {
                                  checkTaskController.getCheckTask(
                                      checkTaskController
                                          .taskModelList[index].id);
                                  checkTaskController.getTaskById(
                                      checkTaskController
                                          .taskModelList[index].id);
                                  Get.toNamed("/detailScreen",
                                      arguments: checkTaskController
                                          .taskModelList[index]);
                                },
                              );
                            },
                          );
                        })),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
